package org.cuit.lmq.service.impl;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.cuit.lmq.domain.po.User;
import org.cuit.lmq.domain.security.LoginUser;
import org.cuit.lmq.service.MenuService;
import org.cuit.lmq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Seon
 * @date 2022/11/12 13:04
 */
@Slf4j
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.getUserByName(username);
        if (ObjectUtils.isNull(user)) {
            throw new RuntimeException("用户名或密码错误");
        }
        List<String> permissions = menuService.getPermissionById(user.getId());
        return new LoginUser(user, permissions);
    }
}
